﻿Imports DotSpatial.Controls.Header
Imports DotSpatial.Controls.Docking
Imports DotSpatial.Data
Imports System.ComponentModel.Composition
Imports System.ComponentModel

Public Class SimpleDocking
    Implements IDockManager

    Private forms As New List(Of Form)()

#Region "IDockManager Members"

    ''' <summary>
    ''' Removes the specified panel.
    ''' </summary>
    ''' <param name="key">The key.</param>
    Public Sub Remove(key As String) Implements IDockManager.Remove
        'For Each form As Form In forms
        '    If form.Name = key Then
        '        form.Close()
        '        forms.Remove(form)
        '        Exit For
        '    End If
        'Next
    End Sub

    ''' <summary>
    ''' Occurs when the active panel is changed.
    ''' </summary>
    Public Event ActivePanelChanged As EventHandler(Of DockablePanelEventArgs) Implements IDockManager.ActivePanelChanged

    ''' <summary>
    ''' Selects the panel.
    ''' </summary>
    ''' <param name="key">The key.</param>
    Public Sub SelectPanel(key As String) Implements IDockManager.SelectPanel
    End Sub

    ''' <summary>
    ''' Adds the specified panel.
    ''' </summary>
    ''' <param name="panel"></param>
    Public Sub Add(panel As DockablePanel) Implements IDockManager.Add
        'Add(panel.Key, panel.Caption, panel.InnerControl, panel.Dock)
    End Sub

    ''' <summary>
    ''' Resets the layout of the dock panels to a developer specified location.
    ''' </summary>
    Public Sub ResetLayout() Implements IDockManager.ResetLayout
    End Sub

    Public Event PanelClosed As EventHandler(Of DockablePanelEventArgs) Implements IDockManager.PanelClosed

    Public Event PanelAdded As EventHandler(Of DockablePanelEventArgs) Implements IDockManager.PanelAdded

    Public Event PanelRemoved As EventHandler(Of DockablePanelEventArgs) Implements IDockManager.PanelRemoved

    Public Event PanelHidden(sender As Object, e As DotSpatial.Controls.Docking.DockablePanelEventArgs) Implements DotSpatial.Controls.Docking.IDockManager.PanelHidden

    Public Sub ShowPanel(key As String) Implements DotSpatial.Controls.Docking.IDockManager.ShowPanel
    End Sub

#End Region

#Region "OnPanelRemoved"

    ''' <summary>
    ''' Triggers the PanelRemoved event.
    ''' </summary>
    Public Overridable Sub OnPanelRemoved(ea As DockablePanelEventArgs)
        'this
        RaiseEvent PanelRemoved(Nothing, ea)
    End Sub

#End Region

#Region "OnPanelAdded"

    ''' <summary>
    ''' Triggers the PanelAdded event.
    ''' </summary>
    Public Overridable Sub OnPanelAdded(ea As DockablePanelEventArgs)
        'this
        RaiseEvent PanelAdded(Nothing, ea)
    End Sub

#End Region

#Region "OnPanelClosed"

    ''' <summary>
    ''' Triggers the PanelClosed event.
    ''' </summary>
    Public Overridable Sub OnPanelClosed(ea As DockablePanelEventArgs)
        'this
        RaiseEvent PanelClosed(Nothing, ea)
    End Sub

#End Region

#Region "OnActivePanelChanged"

    ''' <summary>
    ''' Triggers the ActivePanelChanged event.
    ''' </summary>
    Public Overridable Sub OnActivePanelChanged(ea As DockablePanelEventArgs)
        'this
        RaiseEvent ActivePanelChanged(Nothing, ea)
    End Sub

#End Region

    ' ''' <summary>
    ' ''' Adds the specified panel.
    ' ''' </summary>
    ' ''' <param name="key">The key.</param>
    ' ''' <param name="caption">The caption of the panel and any tab button.</param>
    ' ''' <param name="panel">The panel.</param>
    ' ''' <param name="dockStyle">The dock location.</param>
    'Public Sub Add(key As String, caption As String, panel As Control, dockStyle As DockStyle)
    '    panel.Dock = dockStyle.Fill

    '    Dim form = New Form()
    '    form.Controls.Add(panel)
    '    form.Name = key
    '    form.Width = panel.Width
    '    form.Height = panel.Height
    '    form.Show()
    '    AddHandler form.Activated, AddressOf form_Activated
    '    forms.Add(form)
    'End Sub

    Private Sub form_Activated(sender As Object, e As EventArgs)
        OnActivePanelChanged(New DockablePanelEventArgs(TryCast(sender, Form).Name))
    End Sub

    Public Sub HidePanel(key As String) Implements IDockManager.HidePanel

    End Sub
End Class

<Export(GetType(IHeaderControl))> _
Public Class SimpleHeaderControl
    Inherits MenuBarHeaderControl
    Implements IPartImportsSatisfiedNotification

    <Import("Shell", GetType(ContainerControl))> _
    Private Property Shell() As ContainerControl

    Public Overrides Sub Add(item As HeaderItem)
        MyBase.Add(item)
    End Sub

    Public Overrides Sub Add(item As DropDownActionItem)
        MyBase.Add(item)
        With item
            If .Key = "kServiceDropDown" Then
                GISForm.mnuLayerBasemap.DropDownItems.Add(.Caption)
            End If
        End With
        AddHandler item.SelectedValueChanged, AddressOf DropDownSelected
    End Sub

    Public Sub DropDownSelected(Sender As Object, e As SelectedValueChangedEventArgs)
        Dim dropdown As DropDownActionItem = Sender
        Dim idx As Integer = dropdown.Items.IndexOf(e.SelectedItem)
        GISForm.mnuLayerBasemap.DropDownItems(idx).PerformClick()
    End Sub


    Public Sub OnImportsSatisfied() Implements IPartImportsSatisfiedNotification.OnImportsSatisfied

    End Sub
End Class

Public Class SimpleStatusControl
    Implements IStatusControl
    Implements IPartImportsSatisfiedNotification
    Private defaultStatusPanel As StatusPanel
    Private statusStrip As StatusStrip

    <Import("Shell", GetType(ContainerControl))> _
    Private Property Shell() As ContainerControl

#Region "IPartImportsSatisfiedNotification Members"

    Public Sub OnImportsSatisfied() Implements IPartImportsSatisfiedNotification.OnImportsSatisfied
        'statusStrip = New StatusStrip()

        'statusStrip.Location = New Point(0, 285)
        'statusStrip.Name = "statusStrip1"
        'statusStrip.Size = New Size(508, 22)
        'statusStrip.TabIndex = 0
        'statusStrip.Text = [String].Empty

        ''adding the status strip control
        'Shell.Controls.Add(Me.statusStrip)

        ''adding one initial status panel to the status strip control
        'defaultStatusPanel = New StatusPanel()
        'Me.Add(defaultStatusPanel)
    End Sub

#End Region

#Region "IStatusControl Members"

    ''' <summary>
    ''' Adds a status panel to the status strip
    ''' </summary>
    ''' <param name="panel">the user-specified status panel</param>
    Public Sub Add(panel As StatusPanel) Implements IStatusControl.Add
        Dim myLabel As New ToolStripStatusLabel()
        myLabel.Name = panel.Key
        myLabel.Text = panel.Caption
        myLabel.Width = panel.Width
        myLabel.Spring = (panel.Width = 0)
        myLabel.TextAlign = ContentAlignment.MiddleLeft

        AddHandler panel.PropertyChanged, Sub(sender As Object, e As PropertyChangedEventArgs)
                                              Dim item = TryCast(sender, StatusPanel)
                                              myLabel.Text = item.Caption
                                              myLabel.Width = item.Width
                                          End Sub

        statusStrip.Items.Add(myLabel)
    End Sub

    <DebuggerStepThrough>
    Public Sub Progress(key As String, percent As Integer, message As String) Implements IStatusControl.Progress
        Exit Sub
        If percent = 0 Then
            defaultStatusPanel.Caption = message
        Else
            defaultStatusPanel.Caption = [String].Format("{0}... {1}%", message, percent)
        End If

        If Not statusStrip.InvokeRequired Then
            ' most actions happen on one thread and the status bar never repaints itself until the end of a process unless
            ' we call Application.DoEvents() or refresh the control.
            statusStrip.Refresh()
        End If
    End Sub

    Public Sub Remove(panel As StatusPanel) Implements IStatusControl.Remove
        statusStrip.Items.RemoveByKey(panel.Key)
    End Sub

#End Region
End Class
